package blue.mqtt4j.core.exception;

import java.text.MessageFormat;

/**
 * @author Jin Zheng
 * @since 2020-03-21
 */
public class MqttException extends RuntimeException
{
	private static final long serialVersionUID = 1L;

	private final int code;

	public MqttException(String message)
	{
		super(message);
		this.code = ErrorCode.SYSTEM_ERROR.getCode();
	}

	public MqttException(Throwable cause)
	{
		super(cause);
		this.code = ErrorCode.SYSTEM_ERROR.getCode();
	}

	public MqttException(ErrorCode errorCode)
	{
		super(errorCode.getMessage());
		this.code = errorCode.getCode();
	}

	public MqttException(ErrorCode errorCode, Object...args)
	{
		super(MessageFormat.format(errorCode.getMessage(), args));
		this.code = errorCode.getCode();
	}

	public int getCode()
	{
		return code;
	}

	public String getStringCode()
	{
		return String.valueOf(code);
	}
}
